package com.xs.langchain4j_springboot.service;

import com.aliyun.ocr_api20210707.models.RecognizeBasicResponse;
import com.aliyun.tea.TeaException;
import com.xs.langchain4j_springboot.config.AiConfig;
import com.xs.langchain4j_springboot.config.PdfAuditAgent;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import jakarta.annotation.Resource;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.View;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

@Service
public class FileReaderService {
    @Autowired
    QwenChatModel qwenChatModel;

    @Autowired
    QwenStreamingChatModel qwenStreamingChatModel;
    @Autowired
    private View error;

    @Autowired
    AiConfig.Assistant assistant;

    @Autowired
    PdfAuditAgent.auditAgent auditAgent;

    @Autowired
    AiConfig.AssistantUnique assistantUnique;

    @Resource
    DownLoadFileService downLoadFileService;

    public String readPdfFile(String pdfUrl) throws IOException {
        InputStream in = new URL(pdfUrl).openStream();
        PDDocument document = PDDocument.load(in);
        PDFTextStripper stripper = new PDFTextStripper();
        String content = stripper.getText(document);
        System.out.println(content);
        return content;
    }


    public String readPictureContent(String picUrl) {
        try {
            com.aliyun.ocr_api20210707.Client client = createClient();
            com.aliyun.ocr_api20210707.models.RecognizeBasicRequest recognizeBasicRequest = new com.aliyun.ocr_api20210707.models.RecognizeBasicRequest()
                .setUrl(picUrl)
                .setNeedRotate(false);

            RecognizeBasicResponse response = client.recognizeBasicWithOptions(recognizeBasicRequest, new com.aliyun.teautil.models.RuntimeOptions());
//            System.out.println(response.getBody().getData());
            return com.aliyun.teautil.Common.toJSONString(response.getBody().getData());
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

    public com.aliyun.ocr_api20210707.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tNLWM2RgD9zRheq1FyQ")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("6eBnhUzOULHR2q9eqJ5e5FZmomnR6o");
        // 默认公网接入地址为"ocr-api.cn-hangzhou.aliyuncs.com"，若您需要使用vpc域名访问，请确保您的ecs建立在杭州region，vpc接入地址为"ocr-api-vpc.cn-hangzhou.aliyuncs.com"
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }
}
